import { NextResponse } from 'next/server';
import productModule from '@/modules/product';

export async function GET() {
  const {products} = await productModule.getAll(999999999, 0);
  const urls = products.map(p => `<url> 
    <loc>${process.env.NEXT_PUBLIC_BASE_URL}/products/${p?.slug}</loc> 
    <lastmod>${p?.updated_at}</lastmod> 
    <changefreq>daily</changefreq>
    <image:image>
      <image:title>${p?.title}</image:title>
      <image:loc>${process.env.NEXT_PUBLIC_BASE_URL}/${p?.cover}</image:loc>
      <image:caption>${p?.description}</image:caption>
    </image:image>
    </url>` ).join('\n');
  const xml = `<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"> ${urls} </urlset>`;
  return new NextResponse(xml, { headers: { 'Content-Type': 'application/xml' }, });
}